 package apiTCP;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import Client.InterfaceClientSocket;
import Factory.SocketFactory;
import Server.InterfaceServerSocket;
import Server.ServerSocketTCP;
import java.io.*;
import java.net.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * 
 * @author manelv8
 */
public class ServerAPI {

	public static void main(String[] args) {
		try {
			int porta = 8080;
                        InterfaceServerSocket server1 =  SocketFactory.getServer(SocketFactory.TCP,porta);
                                              
			System.out.println("server on line");
//			Socket socket = server.accept();
                        while(true){
                         InterfaceClientSocket client = server1.aceitar();
			System.out.println("conectado...");

			//responde(socket);
                        
                        String teste = server1.receberMsg(client);
                        System.out.println("mensagem recebida API: " + teste );
                        
                        System.out.println("enviando eco...");
                        server1.enviarMsg(teste,client);
                        }
			//server1.close();

		} catch (IOException ex) {
			Logger.getLogger(ServerAPI.class.getName())
					.log(Level.SEVERE, null, ex);
		}

	}

	private static void responde(Socket socket) {
                
            
            
//		try {
                    
                    
			//BufferedReader rd = new BufferedReader(new InputStreamReader(
			//		socket.getInputStream()));
			//PrintStream writer = new PrintStream(socket.getOutputStream());

			//String linha = rd.readLine(); 
		//	String retorno = "";

		//	while (linha != null && !(linha.trim().equals(""))) {  
		//		System.out.println("server: enviando para ->"
		//				+ socket.getInetAddress().getHostName());
				// envia a linha de volta.  
		//		writer.println("Eco: " + linha);
		//		System.out.println("server: enviado");
                // espera por uma nova linha.  
              //  linha = rd.readLine();  
             //   }  
/*
			System.out.println("preparando envio....");

			System.out.println("server: enviando para ->"
					+ socket.getInetAddress().getHostName());
			System.out.println("server:" + socket.getPort());
			writer.println(retorno);

			System.out.println("server: enviado");

			writer.flush();
			rd.close();
			writer.close();
*/
//		} catch (IOException ex) {
//			System.out.println("erro de io");
//			ex.printStackTrace();
//		}

	}
}
